package net.vkits.controller;

import net.vkits.auth.application.UserApplicationService;
import net.vkits.auth.domain.model.User;
import net.vkits.common.Utils;
import net.vkits.common.dto.UserDTO;
import net.vkits.common.dto.UserRoleDTO;
import net.vkits.common.validations.UserDTOValidFirst;
import net.vkits.common.validations.UserDTOValidSecond;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 16/3/27.
 */
@Controller
@RequestMapping("/users")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource
    UserApplicationService userApplicationService;

    @RequiresPermissions("system:sys_user:list")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView usersList() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "system");
        modelMap.put("sidebar", "users");
        modelMap.put("currentUser",username);
        return new ModelAndView("/users", modelMap);
    }

    @RequiresPermissions("system:sys_user:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<User> getUsers(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<User> users = userApplicationService.getUserList(pageable);
        log.info("size is: {}", users.getTotalElements());
        return users;
    }

    @RequiresPermissions("system:sys_user:add")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String addUser(@RequestBody @Validated({UserDTOValidFirst.class, UserDTOValidSecond.class}) UserDTO user) {
        log.info("add User: {}", user);
        userApplicationService.addUser(user.getName(), user.getPassword());
        return "done";
    }

    @RequiresPermissions("system:sys_user:modify")
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public String setUserRole(@RequestBody UserRoleDTO userRoleDTO) {
        log.info("setUserPermission: {}", userRoleDTO);
        userApplicationService.setUserRole(userRoleDTO.getUsername(), userRoleDTO.getRoleIds());
        return "done";
    }

    @RequiresPermissions("system:sys_user:modify")
    @RequestMapping(value = "resetPassword", method = RequestMethod.GET)
    @ResponseBody
    public String resetPassword(@RequestParam(value = "username") String username) {
        log.info("get Username: {}", username);
        userApplicationService.resetPassword(username);
        return "done";
    }
}
